package net.caixiaomi.info.Lottery.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.caixiaomi.info.Lottery.adapter.LotteryBettingAdapter;
import net.caixiaomi.info.Lottery.callback.LotteryBallBettingCallBack;
import net.caixiaomi.info.Lottery.model.LotteryBettingEntity;
import net.caixiaomi.info.Lottery.presenter.LotteryBettingPresenter;
import net.caixiaomi.info.R;
import net.caixiaomi.info.WebViewActivity;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.BaseActivity;
import net.caixiaomi.info.base.BaseAlertDialog;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.helper.AppHelper;
import net.caixiaomi.info.model.CommonEvent;
import net.caixiaomi.info.ui.dialog.TrebleDialogFragment;
import net.caixiaomi.info.ui.football.PaymentActivity;
import net.caixiaomi.info.ui.login.LoginActivity;
import net.caixiaomi.info.util.Constants;
import net.caixiaomi.info.util.EventWhat;
import net.caixiaomi.info.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by suntian on 2018/8/10.
 */

public class LotteryBettingConfirmActivity extends BaseActivity implements BaseQuickAdapter.OnItemChildClickListener,LotteryBallBettingCallBack {

    private Context mContext;
    private int mTrebleNum = 1;//倍数
    private int mType = Constants.TYPE_BALL_LOTTERY;

    private List<MultiItemEntity> mBettingList;
    private LotteryBettingEntity footerEntity;


    @BindView(R.id.lottery_betting_multiple)
    RelativeLayout mMultipleLayout;

    @BindView(R.id.lottery_betting_btn)
    RelativeLayout mSubBtn;

    @BindView(R.id.lottery_betting_multiple_tv)
    TextView mMultipleTv;

    @BindView(R.id.lottery_add_chip)
    LinearLayout mAddChipLayout;

    @BindView(R.id.lottery_betting_total_des)
    TextView mTotalDes;

    @BindView(R.id.lottery_add_chip_img)
    ImageView mAddChipImg;

    @BindView(R.id.lottery_betting_listview)
    RecyclerView mListView;

    public LotteryBettingAdapter mAdapter;

    private LotteryBettingPresenter mPresenter;
    private boolean mNoticeSelected=true;   //同意协议
    private boolean mAddChip=false;  //追加投注

    private int mFromType=0;

    @OnClick(R.id.lottery_betting_btn)
    void betting(){
        String token = CommonApp.getToken();
        if (TextUtils.isEmpty(token)) {
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            return;
        }

//        if (!mBtnSelected.isSelected()) {
//            ToastUtil.s(R.string.C_LOTTERY_SERVICE_PROTOCOL_TIP);
//            return;
//        }
        mPresenter.betting();
//        Intent intent=new Intent(mContext,LotteryOrderDetailActivity.class);
//        startActivity(intent);
    }

    @OnClick(R.id.lottery_add_chip)
    void addChip(){
        mAddChip=!mAddChip;
        if(mAddChip){
            mAddChipImg.setBackgroundResource(R.drawable.selected_box_img);
        }else{
            mAddChipImg.setBackgroundResource(R.drawable.selection_box_img);
        }
        mAdapter.setChips(mAddChip);
        mPresenter.setParams(mTrebleNum,mAddChip);
        mTotalDes.setText(Html.fromHtml(mPresenter.getTotalDes()));
    }

    @OnClick(R.id.lottery_betting_multiple)
    void multiple(){
        TrebleDialogFragment trebleDialog = new TrebleDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("treble", mTrebleNum);
        trebleDialog.setArguments(bundle);
        trebleDialog.show(getSupportFragmentManager(), TrebleDialogFragment.class.getName());
    }

    @OnClick(R.id.lottery_bet_select0)
    void select0(){
        CommonApp.mLotteryIndex=-1;
        CommonApp.mAddChip=mAddChip;
        CommonApp.mTrebleNum=mTrebleNum;
        Intent intent=new Intent(mContext, BallLotteryMainActivity.class);
        startActivity(intent);
//        finish();
    }

    @OnClick(R.id.lottery_bet_select1)
    void select1(){
        List<LotteryBettingEntity> list=mPresenter.getRandomCount(1);
        CommonApp.mLotteryBettingList.addAll(list);
        mBettingList.clear();
        mBettingList.addAll(CommonApp.mLotteryBettingList);
        mBettingList.add(footerEntity);
        mAdapter.setNewData(mBettingList);
        mAdapter.notifyDataSetChanged();
        mTotalDes.setText(Html.fromHtml(mPresenter.getTotalDes()));
    }

    @OnClick(R.id.lottery_bet_select2)
    void select2(){
        List<LotteryBettingEntity> list=mPresenter.getRandomCount(5);
        CommonApp.mLotteryBettingList.addAll(list);
        mBettingList.clear();
        mBettingList.addAll(CommonApp.mLotteryBettingList);
        mBettingList.add(footerEntity);
        mAdapter.setNewData(mBettingList);
        mAdapter.notifyDataSetChanged();
        mTotalDes.setText(Html.fromHtml(mPresenter.getTotalDes()));
    }

    @Override
    protected int layout() {
        return R.layout.lottery_betting_confirm_layout;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        mBettingList.clear();
        mBettingList.addAll(CommonApp.mLotteryBettingList);
        mBettingList.add(footerEntity);
        mAdapter.setNewData(mBettingList);
        mAdapter.notifyDataSetChanged();
        mTotalDes.setText(Html.fromHtml(mPresenter.getTotalDes()));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=this;
        mPresenter=new LotteryBettingPresenter(mContext,this);
        mAdapter=new LotteryBettingAdapter(null);
        mListView.setLayoutManager(new LinearLayoutManager(this));
        mListView.setAdapter(mAdapter);

        mBettingList=new ArrayList<>();
        mBettingList.addAll(CommonApp.mLotteryBettingList);

        footerEntity=new LotteryBettingEntity();
        footerEntity.setType(Constants.ITEM_LOTTERY_BEETTING_MORE);
        mBettingList.add(footerEntity);
        mAdapter.setNewData(mBettingList);
        mAdapter.notifyDataSetChanged();
        mAdapter.setOnItemChildClickListener(this);
        mAdapter.setSelected(mNoticeSelected);
        mAddChip=CommonApp.mAddChip;
        mTrebleNum=CommonApp.mTrebleNum;

        mAdapter.setChips(mAddChip);
        mMultipleTv.setText("倍数 "+mTrebleNum+"倍");
        mAdapter.setmTrebleNum(mTrebleNum);
        mPresenter.setParams(mTrebleNum,mAddChip);

        mMultipleTv.setText("倍数 "+mTrebleNum+"倍");
        if(mAddChip){
            mAddChipImg.setBackgroundResource(R.drawable.selected_box_img);
        }else{
            mAddChipImg.setBackgroundResource(R.drawable.selection_box_img);
        }

        mTotalDes.setText(Html.fromHtml(mPresenter.getTotalDes()));
        if(mAddChip){
            mAddChipImg.setBackgroundResource(R.drawable.selected_box_img);
        }else{
            mAddChipImg.setBackgroundResource(R.drawable.selection_box_img);
        }
        mFromType=getIntent().getIntExtra("type",-1);
    }

    @Override
    public void onPostEvent(CommonEvent event) {
        try {
            switch (event.getWhat()) {
                case EventWhat.EVENT_TREBLE://选择倍数
                    mTrebleNum = Integer.valueOf(event.getMessage());
                    mMultipleTv.setText("倍数 "+mTrebleNum+"倍");
                    mAdapter.setmTrebleNum(mTrebleNum);
                    mPresenter.setParams(mTrebleNum,mAddChip);
                    mTotalDes.setText(Html.fromHtml(mPresenter.getTotalDes()));
                    break;
                case EventWhat.EVENT_PAYMENT:
//                    Intent intent=new Intent(mContext,LotteryOrderDetailActivity.class);
//                    startActivity(intent);
                    finish();
                    break;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onBackPressed() {

        if (getCurrentFocus() != null) {
            AppHelper.hideKeyboard(getCurrentFocus());
        }

        BaseAlertDialog dialog=new BaseAlertDialog(mContext);
        dialog.setTitle("温馨提示");
        dialog.setMessage("返回将清空所选号码，确定返回？");
        dialog.setPositiveButton("确定", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonApp.mLotteryBettingList.clear();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    finishAfterTransition();
                } else {
                    finish();
                }
                if(CommonApp.mLotteryIndex!=-2){
                    Intent intent=new Intent(mContext, BallLotteryMainActivity.class);
                    if(mFromType==1){
                        intent.putExtra("type",1);
                    }
                    startActivity(intent);
                }
                CommonApp.mLotteryIndex=-1;
                CommonApp.mAddChip=false;
                CommonApp.mTrebleNum=1;
            }
        });
        dialog.setNegativeButton("取消", new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        dialog.show();
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

        switch (view.getId()){
            case R.id.lottery_bet_ite_go:
                CommonApp.mLotteryIndex=position;
                CommonApp.mAddChip=mAddChip;
                CommonApp.mTrebleNum=mTrebleNum;
                Intent intent=new Intent(mContext, BallLotteryMainActivity.class);
                mContext.startActivity(intent);
//                finish();
                break;
            case R.id.lottery_bet_ite_delete:
                CommonApp.mLotteryBettingList.remove(position);
                mBettingList.clear();
                mBettingList.addAll(CommonApp.mLotteryBettingList);
                mBettingList.add(footerEntity);
                mAdapter.setNewData(mBettingList);
                mAdapter.notifyDataSetChanged();
                mTotalDes.setText(Html.fromHtml(mPresenter.getTotalDes()));
                if(CommonApp.mLotteryBettingList.size()==0){
                    CommonApp.mLotteryIndex=-1;
                    Intent deldete=new Intent(mContext, BallLotteryMainActivity.class);
                    if(mFromType==1){
                        deldete.putExtra("type",1);
                    }
                    mContext.startActivity(deldete);
                    finish();
                }
                break;
            case R.id.btn_selected:
                mNoticeSelected=!mNoticeSelected;
                mAdapter.setSelected(mNoticeSelected);
                if(mNoticeSelected){
                    mSubBtn.setBackgroundResource(R.color.orange_primary);
                }else{
                    mSubBtn.setBackgroundResource(R.color.divider);
                }
                break;
            case R.id.btn_protocol:
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(Constants.BASE_H5_URL).append(getString(R.string.H5_BUY_PROTOCOL));
                String url = stringBuilder.toString();
                Intent protocol = new Intent(mContext, WebViewActivity.class);
                protocol.putExtra("url", url);
                startActivity(protocol);
                break;
        }
    }


    @Override
    public void betSUccess(String payToken) {
//        CommonApp.mLotteryIndex=-1;
//        CommonApp.mLotteryBettingList.clear();
        Intent intent=new Intent(mContext,PaymentActivity.class);
        intent.putExtra("payToken",payToken);
        intent.putExtra("type",Constants.TYPE_BALL_LOTTERY);
        startActivity(intent);
    }
}
